package cn.wolfcode.shop.msg;

import cn.wolfcode.shop.vo.OrderVo;
import com.alibaba.fastjson.JSON;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import java.util.HashMap;
import java.util.Map;

/**
 * @Component 交给springboot来管理,创建单例对象
 * 发布消息
 */
@Component
public class OrderInfoProducer {

    @Autowired
    private JmsTemplate jmsTemplate;
    //定义目的地, 一个生产者只有一个目的地
    private Destination destination= new ActiveMQQueue("shop.order");
    //private Destination destination= new ActiveMQTopic("shop.topic");

    //发布消息
    public void sendMsg(Long userId,OrderVo orderVo) throws JMSException {

        Map<String,Object> param = new HashMap();
        param.put("userId",userId);
        param.put("orderVo", JSON.toJSONString(orderVo));

        //传递过去,需要两个参数(目的地,消息)
        jmsTemplate.convertAndSend(destination,param);
    }

}
